package com.linkage.interceptor;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.linkage.pojo.background.UserRole;
import com.linkage.pojo.consumer.User;
import com.linkage.repository.background.UserManageRepository;

public class LoginInterceptor extends HandlerInterceptorAdapter {
    /**
     * 在Controller方法前进行拦截
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Autowired
    private UserManageRepository userManageRepository;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(!(handler instanceof HandlerMethod)) {
            return super.preHandle(request, response, handler);
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();

        // 获取注解
        NeedLogin nl = method.getAnnotation(NeedLogin.class);
        NeedAdminLogin nal = method.getAnnotation(NeedAdminLogin.class);

        if (nl == null && nal == null) {
            return super.preHandle(request, response, handler);
        }

        if (nl != null) {

            HttpSession session = request.getSession(true);
            User loginUser = (User) session.getAttribute("LOGIN_USER_KEY_IN_SESSION");

            if (loginUser == null) {
                response.sendRedirect(request.getContextPath() + "/gotoLogin");
            } else {
                request.setAttribute("loginUser", loginUser);
                return super.preHandle(request, response, handler);
            }
        }

        if (nal != null) {

            HttpSession session = request.getSession(true);
            User adminLoginUser = (User) session.getAttribute("ADMIN_USER_KEY_IN_SESSION");
            String adminRoleName = (String) session.getAttribute("ADMIN_ROLE_NAME_IN_SESSION");
            
            
            if (adminLoginUser == null || StringUtils.isEmpty(adminRoleName) ) {
                response.sendRedirect(request.getContextPath() + "/admin/adminlogin");
            } else {
                request.setAttribute("adminLoginUser", adminLoginUser);
                request.setAttribute("adminRoleName", adminRoleName);
                return super.preHandle(request, response, handler);
            }
            
            
       

        }

        return true;

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        request.setAttribute("loginUser", request.getSession().getAttribute("LOGIN_USER_KEY_IN_SESSION"));
        request.setAttribute("adminLoginUser", request.getSession().getAttribute("ADMIN_USER_KEY_IN_SESSION"));
        request.setAttribute("adminRoleName", request.getSession().getAttribute("ADMIN_ROLE_NAME_IN_SESSION"));
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        request.setAttribute("loginUser", request.getSession().getAttribute("LOGIN_USER_KEY_IN_SESSION"));
        request.setAttribute("adminLoginUser", request.getSession().getAttribute("ADMIN_USER_KEY_IN_SESSION"));
        request.setAttribute("adminRoleName", request.getSession().getAttribute("ADMIN_ROLE_NAME_IN_SESSION"));
        super.afterCompletion(request, response, handler, ex);
    }
}
